/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import data.PrestasiData;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Muhammad Dony
 */
public class PrestasiDB extends ConnectDB {

    private String sql;
    private Connection con;
    private Statement stmt;
    private ResultSet rs;
    
    public List getprestasi() throws SQLException, ClassNotFoundException{
        sql = "SELECT * from `prestasi`";
        return getprestasidata(sql);
    }
    
    public List getprestasiById(String ID) throws SQLException, ClassNotFoundException {
        sql = "SELECT * from `prestasi` WHERE `ID`='"+ID+"';";
        return getprestasidata(sql);
    }
     
    public List getprestasiByxx(String ID, int no) throws SQLException, ClassNotFoundException {
        sql = "SELECT * from `prestasi` WHERE `ID`='"+ID+"' AND `nomor`='"+no+"';";
        return getprestasidata(sql);
    }
    
     private List getprestasidata(String sql) throws SQLException, ClassNotFoundException{
        // mempersiapkan list dataPelaporan
        List listprestasi = new ArrayList();
        //membuat koneksi , createConnection terdapat dalam DBClass
        con = createConnection();
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        // mempersiapkan object dataPelaporan
        PrestasiData prestasiUser;
        
        while(rs.next()){
            prestasiUser = new PrestasiData();
            prestasiUser.setNomor(rs.getInt(1));
            prestasiUser.setID(rs.getString(2));
            prestasiUser.setPrestasi(rs.getString(3));
            prestasiUser.setKegiatan(rs.getString(4));
            prestasiUser.setTingkat(rs.getString(5));
            prestasiUser.setTahun(rs.getString(6)); 
            prestasiUser.setKategori(rs.getString(7));
            prestasiUser.setStatus(rs.getString(8));
            listprestasi.add(prestasiUser);
        }
        // menutup semua koneksi db
        rs.close();
        stmt.close();
        con.close();
        //return value list data pelaporan
        return listprestasi;
        
    }
    
    private void actprestasi(String sql) throws ClassNotFoundException, SQLException{
        // membuat koneksi
        con = createConnection();
        stmt = con.createStatement();
        // execute query
        stmt.execute(sql);
        // menutup koneksi db
        stmt.close();
        con.close();
    }
   public void insertPrestasi(PrestasiData listPrestasi) throws ClassNotFoundException, SQLException{
        // query
        sql = "INSERT INTO `scholarship`.`prestasi` (`ID`, `prestasi`, `kegiatan`, `tingkat`, `tahun`, `kategori`, `status`) VALUES ('"
                +listPrestasi.getID()+"', '"
                +listPrestasi.getPrestasi()+"', '"
                +listPrestasi.getKegiatan()+"', '"
                +listPrestasi.getTingkat()+"', '"
                +listPrestasi.getTahun()+"', '"
                +listPrestasi.getKategori()+"', '"
                +listPrestasi.getStatus()+"');";
        actprestasi(sql);
    }
   

   //// EDIT DAN DELETE MASIH BERMASALAH Y!!!!!
   
   
    public void updatePrestasi(PrestasiData listPrestasi) throws ClassNotFoundException, SQLException{
        // query
        sql = "UPDATE `prestasi` SET `prestasi`='"+listPrestasi.getPrestasi()+
                "',`kegiatan`='"+listPrestasi.getKegiatan()+
                "',`tingkat`='"+listPrestasi.getTingkat()+
                "',`tahun`='"+listPrestasi.getTahun()+
                "',`kategori`='"+listPrestasi.getKategori()+
                "',`status`='"+listPrestasi.getStatus()+
                "' WHERE `ID`='"+listPrestasi.getID()+"'  AND nomor="+listPrestasi.getNomor()+";";
        // memanggil method untuk execute query
        actprestasi(sql);
    }
    public void deletePrestasi(String ID, int no) throws ClassNotFoundException, SQLException{
        sql = "DELETE FROM `prestasi` WHERE `ID`='"+ID+"' AND `nomor`='"+no+"';";
        actprestasi(sql);
    } 
    
    
    
}
